﻿#include "AppManager.h"
#include <QMainWindow>
#include <QApplication>
#include <QDebug>
#include <ElaWindow.h>
#include <ElaContentDialog.h>
#include "IBaseSignal.h"
#include "QBaseLog.h"

namespace BaseModule
{
    AppManager::AppManager()
    {
        auto _baseSignal = IBaseSignalInstance;

        connect(_baseSignal, &BaseModule::IBaseSignal::signal_sendQuestionMessageToUI, this, &AppManager::slot_sendQuestionMessageToUI);
        connect(_baseSignal, &BaseModule::IBaseSignal::signal_sendErrorMessageToUI, this, &AppManager::slot_sendErrorMessageToUI);
    }

    AppManager::~AppManager()
    {
    }

    void AppManager::init(QMainWindow* mainWindow)
    {
        m_mainWindow = mainWindow;

        // 将打印到控制台的信息转存到日志文件中，release模式有效
#ifdef NDEBUG
        if (!QBaseLog::open_sys_message(qApp->applicationDirPath() + "/log"))
        {
            printf("create log file fail");
            slot_sendErrorMessageToUI(QStringLiteral("创建日志文件失败."));
            return;
        }
        qInstallMessageHandler(QBaseLog::SysMessageOutput);
        qDebug() << "\n\n========================The program start running========================";
#endif

        // 窗口关闭事件统一处理
        handleAppClose();

    }


    void AppManager::slot_sendQuestionMessageToUI(int& result, const QString& title, const QString& message)
    {
        auto _closeDialog = new ElaContentDialog(m_mainWindow);
        _closeDialog->setTitleText(title);
        _closeDialog->setContentText(message);
        _closeDialog->setUseMultilineText(true);

        connect(_closeDialog, &ElaContentDialog::leftButtonClicked, [=]() { _closeDialog->reject(); }); //取消
        connect(_closeDialog, &ElaContentDialog::rightButtonClicked, [=]() { _closeDialog->accept(); });    //确定

        result = _closeDialog->exec();
    }

    void AppManager::slot_sendErrorMessageToUI(const QString& message)
    {
        auto _closeDialog = new ElaContentDialog(m_mainWindow);
        _closeDialog->setTitleText(QStringLiteral("错误"));
        _closeDialog->setContentText(message);
        _closeDialog->setUseMultilineText(true);
        _closeDialog->setLeftButtonVisible(false);
        _closeDialog->exec();
    }

    void AppManager::handleAppClose()
    {
        auto _mainWindow = qobject_cast<ElaWindow*>(m_mainWindow);
        // 拦截默认关闭事件
        auto _closeDialog = new ElaContentDialog(m_mainWindow);
        _closeDialog->setContentText(QStringLiteral("确定要退出程序吗？"));
        _closeDialog->setMiddleButtonVisible(true);
        connect(_closeDialog, &ElaContentDialog::middleButtonClicked, _mainWindow, &ElaWindow::showMinimized);
        connect(_closeDialog, &ElaContentDialog::rightButtonClicked, _mainWindow, &ElaWindow::closeWindow);
        _mainWindow->setIsDefaultClosed(false);
        connect(_mainWindow, &ElaWindow::closeButtonClicked, this, [=]() {
            bool isClose = true; //true允许，false禁止
            emit IBaseSignalInstance->signal_readyCloseApp(isClose);
            if (isClose)
            {
                _closeDialog->exec();
            }
            });
    }


}
